﻿/**
 * In4jDataSource.java 1.0 2007-4-26 上午12:48:24,创建 by yangfan
 */


package org.in4j.bbs;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 *
 * @author yangfan
 * @version 1.0 2007-4-26 上午12:48:24,创建
 */
public class In4jDataSource extends DriverManagerDataSource
{
	private static final Log log = LogFactory.getLog(In4jDataSource.class);

	private int i = 0;
	@Override
	public Connection getConnection() throws SQLException
	{
		log.debug("----------open connection[" + (++i) + "]");
		Connection targetConn = super.getConnection();
		ProxyFactory factory = new ProxyFactory(targetConn);
		NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor();
		advisor.addMethodName("close");
		advisor.addMethodName("commit");
		advisor.addMethodName("rollback");
		advisor.setAdvice(new MethodBeforeAdvice()
		{

			public void before(Method method, Object[] args, Object target) throws Throwable
			{
				String methodName = method.getName();
				log.debug("!-------------in4j [" + methodName + "] connection[" + i + "]-----");
			}
		});
		factory.addAdvisor(advisor);
		Connection connection = (Connection)factory.getProxy();
		return connection;
	}
}
